<html>
<?php
    require_once("menu.php");
	require_once('controllers/user.php');

	$user = User::getCurrentUser();
	$html = "";	
	$err_msg="";
	$succ_msg = "";
	if($user == null){
		$html = "<div style='color:red;font-size:14px;'>Devi essere autenticato per poter cambiare password </div>";
	}else{
		if(isset($_POST['txtNewPassword'])){
			// imposta la nuova password
			
			try{
				$user->changePassword($_POST['txtNewPassword'], $_POST['txtOldPassword']);
				$succ_msg = 'La password &egrave; stata cambiata con successo';
			}catch (Exception $ex){
				$err_msg = $ex->getMessage();
			}
		}	
	}
	
	

?>
	<head>
		<title>Fantacalcio - Cambia password</title>
		<link rel="stylesheet" href="fantacalcio.css" type="text/css"/>
		<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
		<style>
			td.label {
				color:blue;
				width:120px;
				font-size:12px;
				text-align:right;
				height:30px;
			}
			
			input.textbox{
				width:200px;
				color:black;
			}
			
			div.label{
				font-weight:bold;
				color:forestgreen;
				height:50px;
				vertical-align:middle;
			}
			
			td.col{
				border-top: solid 1px black;
				border-right: solid 1px black;
			}
			
			td.sx{
				border-left: solid 1px black;
			}
			
			td.bottom{
				border-bottom:solid 1px black;
			}
			
			div.error{
				text-align:left;
				color:red;
				font-style:italic;
				font-size:14px;
			}
			
			td.success{
				text-align:left;
				color:brown;
				font-weight:bold;
				font-size:16px;
			}
			
		</style>
		
		<script language='javascript' type='text/javascript'>
			
			function doPostBack(){
			// controlla prima se le due nuove password inserite sono corrette
				pwd1 = document.getElementById('txtNewPassword').value;
				pwd2 = document.getElementById('txtRepPassword').value;
				if(pwd1==pwd2){
					document.getElementById('f_chg_pwd').submit();
				}else{
					document.getElementById('imgError').style.visibility='visible';		
					document.getElementById('dvError').innerHTML = 'La seconda password digitata non corrisponde alla prima';
				}
			}
		</script>
	</head>
	<body>
		<table class="pageTable" cellpadding='0' cellspacing='0'>
			<tr>
				<td>
					<?php echo printMasterMenu('');?>
					
				</td>
				
			</tr>
			<tr>
				<td class='contentPage'>
					<table style="width:100%">
						<tr>
							<td class="pageTitle">Modulo cambio password</td>
							
						</tr>
						<tr>
							<td>
								
								<div style='width:95%;height:600px;overflow:hidden;
									border-color:gray;border-style:solid;border-width:1px;text-align:center;'>
									<table style='width:100%;text-align:center;' align="center">
										<?php 
											if($html!=''){
											//se l'utente non è autenticato visualizza un messaggio
												echo $html;
												
											}else { ?>
												
													
													<form id='f_chg_pwd' method='POST' action='chg_pwd.php' >
													<table cellpadding='5' cellspacing='0' style='margin-top:50px;border-color:black;' align='center'>
														<tr>
															<td colspan='3'>
																<div class='label'>Cambia la password del tuo account</div>
															</td>
														</tr>
														<tr>
															<td class='success col sx' colspan='2'>
																<?php echo $succ_msg; ?>
															</td>
															<td class=' '>&nbsp;</td>
														</tr>
														<tr>
															<td class='label col sx'>Utente:</td>
															<td class='col'><?php echo $user->getNome() . " " . $user->getCognome(); ?></td>
															<td class=' '>&nbsp;</td>
														</tr>
														<tr>
															<td class='label col sx'>Nome di accesso:</td>
															<td class='col'><?php echo $user->getAlias(); ?></td>
															<td class=' '>&nbsp;</td>
														</tr>
														<tr>
															<td class='label col sx'>Vecchia password:</td>
															<td class='col'><input class='textbox' type='password' name='txtOldPassword' value='' /></td>
															<td class=''>&nbsp;</td>
														</tr>
														<tr>
															<td class='label col sx'>Nuova password:</td>
															<td class='col'><input class='textbox' type='password' id='txtNewPassword' name='txtNewPassword' value='' /></td>
															<td class=''>&nbsp;</td>
														</tr>
														<tr>
															<td class='label col sx bottom'>Ripeti password:</td>
															<td class='col bottom'>
																<input class='textbox' type='password' id='txtRepPassword' name='txtRepPassword' value='' />
															</td>
															<td class=''><img src='images/warning.jpeg' alt='err' id='imgError' style='visibility:hidden;' /></td>
														</tr>
														<tr>
															<td class='' colspan='2' style='text-align:right'>
																<input type='button' name='btnCambiaPassword' value='Cambia' onclick='doPostBack();' />
															</td>
															<td class=' '>&nbsp;</td>
														</tr>
														<tr>
															<td colspan='3'><div id='dvError' class='error'><?php echo $err_msg; ?></div></td>
														</tr>
													</table>
													</form>
												
										<?php	}

										?>
										<tr>
											
										</tr>
									</table>
									
								</div>
								
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<?php printFooter(); ?>
				</td>
				
			</tr>
		</table>
	</body>
</html>